﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace Rainr.EFClient
{
    /// <summary>
    /// 序列扩展
    /// </summary>
    public static class IEnumerableExtensions
    {
        /// <summary>
        /// 对一个非延迟查询的序列元素进行分页。
        /// </summary>
        /// <typeparam name="TSource">source 中的元素的类型。</typeparam>
        /// <param name="source">一个要分页的值序列。</param>
        /// <param name="pageIndex">页位置。</param>
        /// <param name="pageSize">页大小。</param>
        /// <returns>一个已分页的元素序列。</returns>
        public static IEnumerable<TSource> Pagination<TSource>(this IEnumerable<TSource> source, int pageIndex, int pageSize) where TSource : class
        {
            return source.Skip<TSource>(pageIndex < 1 ? 0 : ((pageIndex - 1) * pageSize)).Take<TSource>(pageSize);
        }
    }
}
